package com.lc.recovery.util;

import android.annotation.TargetApi;
import android.graphics.Outline;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.LinearLayout;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CourseUtils {
    public static int statusbarHeight = 0;
    public static final int DEFAULT_STATUS_BAR_ALPHA = 0;

    /**
     * 判断是否为空
     *
     * @param text
     * @return
     */
    public static boolean isNullOrEmpty(String text) {
        if (text == null || "".equals(text.trim()) || text.trim().length() == 0
                || "null".equals(text.trim())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是不为空
     *
     * @param text
     * @return
     */
    public static boolean isNotNullOrEmpty(String text) {
        if (text == null || "".equals(text.trim()) || text.trim().length() == 0
                || "null".equals(text.trim())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 字符串 转换 int
     * 需要转化的数据
     *
     * @param
     * @return 转化后的int数据
     */

    public static int parseInt(String s) {
        if (canParseInt(s)) {
            return Integer.parseInt(s);
        }
        return 0;
    }

    /**
     * 字符串 转换 long
     * 需要转化的数据
     *
     * @param
     * @return 转化后的int数据
     */

    public static long parseLong(String s) {
        if (canParseLong(s)) {
            return Long.parseLong(s);
        }
        return 0;
    }

    /**
     * 验证一个字符串是否能解析成整数
     *
     * @param numberStr
     * @return
     */
    public static boolean canParseInt(String numberStr) {
        try {
            Integer.parseInt(numberStr);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 验证一个字符串是否能解析成长整型数
     *
     * @param numberStr
     * @return
     */
    public static boolean canParseLong(String numberStr) {
        try {
            Long.parseLong(numberStr);
            return true;
        } catch (NumberFormatException e) {
            return false;
        } catch (NullPointerException e) {
            return false;
        }
    }



    /**
     * 设置视图裁剪的圆角半径 支持所有view  5.0以上系统
     * 注意：传入的view不允许有padding属性存在
     *
     * @param radius
     */
    @Deprecated //使用ViewUtils
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setClipViewCornerRadius(View view, final int radius) {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            //不支持5.0版本以下的系统
            return;
        }

        if (view == null) return;

        if (radius <= 0) {
            return;
        }
        view.setOutlineProvider(new ViewOutlineProvider() {

            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), radius);
            }
        });
        view.setClipToOutline(true);

    }

    /**
     * 获取view高度，单位px；
     *
     * @param view            需要测量的view，
     * @param isIncludeMargin 是否包含topMargin和bottomMargin
     * @return view的高度
     */
    public static int getViewHeight(View view, boolean isIncludeMargin) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        int margin = 0;
        if (isIncludeMargin) {
            try {
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                margin = layoutParams.topMargin + layoutParams.bottomMargin;
            } catch (Exception e) {
                margin = 0;
            }
        }
        return view.getMeasuredHeight() + margin;
    }





    /**
     * 获取现在时间
     */
    public static Date getNowDate() {
        Date date = new Date(System.currentTimeMillis());
        return date;
    }
    public static String date2TimeStamp(String date_str,String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);

            Log.e("TAG", "date2TimeStamp: "+String.valueOf(sdf.parse(date_str).getTime()/1000));
            return String.valueOf(sdf.parse(date_str).getTime()/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 计算两个时间差
     *
     * @param endDate
     * @param nowDate
     * @return
     */

    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        String finalHour;
        String finalMin;
        String finalSec;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        finalHour = hour < 10 ? "0" + hour : hour + "";
        finalMin = min < 10 ? "0" + min : min + "";
        finalSec = sec < 10 ? "0" + sec : sec + "";
        return finalHour + "小时" + finalMin + "分" + finalSec + "秒";

    }

    /**
     * 将秒数转换为hh:mm:ss的格式
     */
    public static String tryTimeConversion(int time) {
        String formatTime;

        long h, m, s;
        h = time / 3600;
        m = (time % 3600) / 60;
        s = (time % 3600) % 60;

        if (h == 0) {
            formatTime = asTwoDigit(m) + "分钟" + asTwoDigit(s)+"秒";
        } else {
            formatTime = asTwoDigit(h) + ":" + asTwoDigit(m) + ":" + asTwoDigit(s);
        }
        return formatTime;
    }
    /**
     * 将秒数转换为hh:mm:ss的格式
     */
    public static String timeConversion(long time) {
        String formatTime;

        long h, m, s;
        h = time / 3600;
        m = (time % 3600) / 60;
        s = (time % 3600) % 60;

        if (h == 0) {
            formatTime = asTwoDigit(m) + ":" + asTwoDigit(s);
        } else {
            formatTime = asTwoDigit(h) + ":" + asTwoDigit(m) + ":" + asTwoDigit(s);
        }
        return formatTime;
    }

    private static String asTwoDigit(long digit) {
        String value = "";

        if (digit < 10) {
            value = "0";
        }

        value += String.valueOf(digit);
        return value;
    }


    /**
     * 毫秒转化时间格式 "yyyy年MM月dd日"
     *
     * @param mills
     * @return
     */
    public static String getMillsToTime(long mills) {
        String liveTime = "";
        Date startTime = new Date(mills);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
        liveTime = sdf.format(startTime);
        return liveTime;

    }
    /**
     * 将String 集合逗号分隔
     *
     * @param list
     * @return
     */
    public static String listToString1(List<String> list) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i < list.size() - 1) {
                    sb.append(list.get(i) + ",");
                } else {
                    sb.append(list.get(i));
                }
            }
        }
        return sb.toString();
    }
}
